package work.mediway.mdm.biz.service.biz.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.PageUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import work.mediway.mdm.core.page.QueryRequest;
import work.mediway.mdm.biz.service.biz.DictLogService;
import work.mediway.mdm.core.constant.MdmConstant;
import work.mediway.mdm.core.constant.SQLConstant;
import work.mediway.mdm.core.exception.DataException;
import work.mediway.mdm.biz.dto.biz.DictLogDTO;

/**
 * @author zhongyj <1126834403@qq.com><br/>
 * @date 2021/5/6
 */
@Slf4j
@Service
public class DictLogServiceImpl implements DictLogService {

    @Resource
    private JdbcTemplate jdbcTemplate;

    @SuppressWarnings("ConstantConditions")
    @Override
    public Page<Map<String, Object>> page(DictLogDTO dictLogDTO, QueryRequest queryRequest) {
        if (StrUtil.isBlank(dictLogDTO.getTableCode())) {
            throw new DataException("表名称不能为空");
        }
        PageUtil.setFirstPageNo(1);
        int start = PageUtil.getStart(queryRequest.getCurrent(), queryRequest.getSize());

        String sql = "SELECT * FROM " + dictLogDTO.getTableCode();
        List<String> where = CollUtil.newArrayList();
        //条件
        if (StrUtil.isNotBlank(dictLogDTO.getParams()) && !JSONUtil.isJsonObj(dictLogDTO.getParams())) {
            throw new DataException(StrUtil.format("【params = {}】不是一个JSON对象", dictLogDTO.getParams()));
        } else if (StrUtil.isNotBlank(dictLogDTO.getParams()) && JSONUtil.isJsonObj(dictLogDTO.getParams())) {
            JSONUtil.parseObj(dictLogDTO.getParams()).forEach((k, v) -> where.add(" " + k + " = " + "'" + v + "' "));
        }
        if (StrUtil.isNotBlank(dictLogDTO.getMapUserId())) {
            where.add(" " + MdmConstant.FIELD_MAP_USER_ID + " = " + "'" + dictLogDTO.getMapUserId() + "' ");
        }
        if (CollUtil.isNotEmpty(where)) {
            sql += " WHERE " + StrUtil.join(SQLConstant.SQL_CONJ_AND, where);
        }
        // 分页
        String count = "SELECT count(*) FROM ( " + sql + ")";
        sql = "SELECT * FROM ( " + sql + ") LIMIT " + start + "," + queryRequest.getSize();

        log.info(StrUtil.format("\n====================== 查询字典表日志 =========================\n"
                        + "表代码为：【{}】\n"
                        + "参数为：【{}】\n"
                        + "查询SQL为：【{}】\n"
                        + "查询总数SQL为：【{}】\n"
                        + "============================================================="
                , dictLogDTO.getTableCode(), dictLogDTO.getParams(), sql, count));

        int countNum = this.jdbcTemplate.queryForObject(count, Integer.class);
        List<Map<String, Object>> query = this.jdbcTemplate.queryForList(sql);

        Page<Map<String, Object>> page = new Page<>(queryRequest.getCurrent(), queryRequest.getSize(), countNum);
        page.setRecords(query);
        page.setPages(PageUtil.totalPage(countNum, queryRequest.getSize()));
        return page;
    }
}










